<?php
namespace app\serverfund\logic;

use \app\manage\logic\BaseLogic;

/**
 * 资金操作日志的逻辑层
 * @author adv30 <adv30@126.com> 2017年6月21日16:57:31
 */
class FundOptionLog extends BaseLogic
{
    protected $name = 'fund_optin_log';
    protected $config = 'db_config_fund';
    private $fundOptionLogModel = '\app\serverfund\model\FundOptionLog';

    private $fundFreezeLogModel = '\app\serverfund\model\FundFreezeLog';
    private $currencyFreezeLogModel = '\app\serverfund\model\CurrencyFreezeLog';
    private $integralFreezeLogModel = '\app\serverfund\model\IntegralFreezeLog';

    private $fundUnfreezeLogModel = '\app\serverfund\model\FundUnfreezeLog';
    private $currencyUnfreezeLogModel = '\app\serverfund\model\CurrencyUnfreezeLog';
    private $integralUnfreezeLogModel = '\app\serverfund\model\IntegralUnfreezeLog';

    private $operateReasonModel = '\app\serverfund\model\OperateReason';

    // 所有用户资金操作日志分页数据
    public function list(array $data): array
    {
        $where['status'] = 1;
        $data['type'] && $where['type'] = $data['type'];
        $data['keywords'] && $where['remark|user_id'] = ['like', '%' . $data['keywords'] . '%'];
        
        if($data['startTime']){
            $where['create_time'] = $data['endTime'] ? ['between', [$data['startTime'],$data['endTime']]] : ['egt', $data['startTime']];
        }else{
            $data['endTime'] && $where['create_time'] = ['elt', $data['endTime']];
        } 

        $res = $this->model($this->fundOptionLogModel)
                    ->where($where)
                    ->order('id desc')
                    ->paginate($data['list_rows'], false, ['page'=>$data['page']])
                    ->toArray();
        return auto_return_page($res, '已找到用户资金操作日志', '没有用户资金操作日志');
    }
    // 获取某些user_ids的的资金操作日志(分页)
    public function listByUserIds(array $data): array
    {
        $where['status'] = 1;
        $where['user_id'] = ['IN', $data['user_ids']];
        $res = $this->model($this->fundOptionLogModel)
                    ->where($where)
                    ->order('id desc')
                    ->paginate($data['list_rows'], false, ['page'=>$data['page']])
                    ->toArray();
        return auto_return_page($res, '已找到用户资金操作日志', '没有用户资金操作日志');
    }
    // 获取某个用户某个状态的资金操作日志分页数据
    public function listByUserType(array $data): array
    {
        $where['status'] = 1;
        $where['user_id'] = $data['user_id'];
        if(isset($data['type']) && $data['type']){ $where['type'] = $data['type']; } 
        if(isset($data['money'])){
            if($data['money'] == 'income'){
                $where['money'] = ['gt',0]; 
            }elseif($data['money'] == 'expend'){
                $where['money'] = ['lt',0]; 
            }
        }
        if(isset($data['time'])){
            $threeMonthTime = mktime(0, 0, 0, date("m")-3, date("d"), date("Y"));
            if($data['time'] == 'threeMonthAgo'){   
                $where['create_time'] = ['lt',$threeMonthTime];    //3个月以前
            }elseif($data['time'] == 'threeMonth'){   
                $where['create_time'] = ['gt',$threeMonthTime];    //3个月之内
            }
        }
        $res = $this->model($this->fundOptionLogModel)
                    ->where($where)
                    ->order('id desc')
                    ->paginate($data['list_rows'], false, ['page'=>$data['page']])
                    ->toArray();
        return auto_return_page($res, '已找到用户资金操作日志', '没有用户资金操作日志');
    }

    /*获取管理员冻结资金、积分、亚数币的原因记录*/
    public function freezeOperateReason($data): array 
    {
        $_where['status'] = 1;
        $data['keywords'] && $_where['reason|user_info$.name|opposite_info$.name'] = ['like', '%' . $data['keywords'] . '%'];
        $data['type'] && $_where['type'] = $data['type'];
        $list = $this->model($this->operateReasonModel)
                    ->where($_where)
                    ->order('id desc')
                    ->paginate(null, false, ['page'=>$data['page'], 'list_rows'=>$data['list_rows']])
                    ->toArray();
        if($list['total'] == 0){
            return verify_err(null, '没有符合条件的记录');
        }
        foreach ($list['data'] as $k => &$v) {
            $_whereLog['type'] = 'system';
            $_whereLog['app_id'] = $v['app_id'];
            $_whereLog['user_id'] = $v['user_id'];
            $_whereLog['obj_id'] = $v['id'];
            $_whereLog['status'] = 1;
            $_whereLog['freeze_counts'] = 1;  //一条原因只冻结一次
            if($v['type'] == 'freeze_fund'){
                $v['freeze_status'] = $this->model($this->fundFreezeLogModel)->where($_whereLog)->value('freeze_status');
            }elseif($v['type'] == 'freeze_currency'){
                $v['freeze_status'] = $this->model($this->currencyFreezeLogModel)->where($_whereLog)->value('freeze_status');
            }elseif($v['type'] == 'freeze_integral'){
                $v['freeze_status'] = $this->model($this->integralFreezeLogModel)->where($_whereLog)->value('freeze_status');
            }else{
                $v['freeze_status'] = -1;
            }
        }
        return auto_return_page($list, '成功获取管理员冻结原因记录表');
    }
    /*获取某条原因记录相关的解冻详情*/
    public function reasonUnfreezeInfo($data): array 
    {
        if($data['action'] == 'currency'){
            $model = $this->currencyFreezeLogModel;
            $unfreezeModel = $this->currencyUnfreezeLogModel;
        }elseif($data['action'] == 'fund'){
            $model = $this->fundFreezeLogModel;
            $unfreezeModel = $this->fundUnfreezeLogModel;
        }elseif($data['action'] == 'integral'){
            $model = $this->integralFreezeLogModel;
            $unfreezeModel = $this->integralUnfreezeLogModel;
        }
        $_where['type'] = 'system';
        $_where['app_id'] = $data['app_id'];
        $_where['user_id'] = $data['user_id'];
        $_where['obj_id'] = $data['obj_id'];
        $_where['status'] = 1;
        $_where['freeze_counts'] = 1;
        $info = $this->model($model)->where($_where)->find();
        if($info){
            $info['unfreeze_reason'] = $this->model($unfreezeModel)->where(['freeze_log_id'=>$info['id']])->value('remark');
        }
        return auto_return($info);

    }
}